<?php

namespace app\api\controller;

use library\Controller;
use think\Db;
use think\Request;

/**
 * 用户数据
 * Class task
 * @package app\store\controller
 */
class Task extends Controller
{
    /**
     * 绑定数据表
     * @var string
     */
    public static $CHANNEL = 888;//平台渠道
    protected $table_DB_Platform_Account_library = 'db_platform_accounts';               /*平台账户配置库*/
    protected $table_DB_Platform_Account_operatelog_library = 'db_platform_operatelog';  /*数据统计库*/
    protected $table_DB_Platform_log_library = 'db_platform_log';                        /*平台日志配置库*/
    protected $game_user_real_time_online = 'game_user_real_time_online';                /* 实时用户 */
    protected $accounts_info = 'accounts_info';                                          /* 用户表 */
    protected $user_score_log= 'user_score_log';                                         /* 金币日志表 */
    protected $stat_remain = 'stat_remain';                                              /* 道具日志表 */
    protected $game_Burying_point = 'game_Burying_point';                                /* 打点日志表 */
    protected $game_burying_point_user = 'game_burying_point_user';                      /* 打点用户表 */
    protected function Db_connect_game_Burying_point(){
        $game_Burying_point= Db::connect($this->table_DB_Platform_Account_operatelog_library)->table($this->game_Burying_point);
        return $game_Burying_point;
    }
    protected function Db_connect_game_burying_point_user(){
        $game_burying_point_user= Db::connect($this->table_DB_Platform_Account_operatelog_library)->table($this->game_burying_point_user);
        return $game_burying_point_user;
    }
    protected function Db_connect_stat_remain(){
        $stat_remain= Db::connect($this->table_DB_Platform_Account_library)->table($this->stat_remain);
        return $stat_remain;
    }
    protected function Db_connect_game_user_real_time_online(){
        $day_user_online= Db::connect($this->table_DB_Platform_Account_operatelog_library)->table($this->game_user_real_time_online);
        return $day_user_online;
    }
    protected function Db_connect_accounts_info()
    {
        $accounts_info = Db::connect($this->table_DB_Platform_Account_library)->table($this->accounts_info);
        return $accounts_info;
    }
    protected function Db_connect_user_score_log(){
        $accounts_info= Db::connect($this->table_DB_Platform_log_library)->table($this->user_score_log);
        return $accounts_info;
    }
    public function user_online_task()
    {
        $now = date('Y-m-d H:i:s',time());
        $begin_time =time();
        $arr =user_online_arr();
        $data = [];
        $this->Db_connect_game_user_real_time_online()->query('TRUNCATE game_user_real_time_online');
        foreach ($arr as $key => $value) {
            $user=$this->Db_connect_accounts_info()->field('SpreaderID')->where('UserID',$value['UserID'])->find();
            $data[]=[
                'UserID'=>$value['UserID'],
                'Room'=>room_type($value['GameName'],$value['GameLevel']),
                'NickName'=>$value['NickName'],
                'SpreaderID'=>$user['SpreaderID']
            ];
        }
        $res =$this->Db_connect_game_user_real_time_online()->insertAll($data);
        /*执行时间*/
        $time_end =time()-$begin_time;
        if($res){
            echo "每周任务执行成功success,执行时间消耗".$time_end."s, 开始执行时间 ,".$now;
        }
    }
    public function user_Burying_point(Request $request)
    {
            $param = $request->param();
            if(empty($param['point_ID']) || empty($param['UserID'])){
                puterror(1);
            }
            $user_type =$this->user_type($param['UserID']);
            $res =$this->burying_insert($param['point_ID'],$param['SpreaderID'],$user_type,$param['UserID']);
            if($res=='success'){
                putsuccess(1);
            }
    }
    /*判断是否是用户类型（新增活跃）1、新增用户 2、活跃用户 3、其他类型*/
    function user_type($UserID){
        $begin_time =date('Y-m-d H:i:s',strtotime(date('Y-m-d',time())));
        $after_time = date('Y-m-d H:i:s',strtotime(date('Y-m-d',time()+24*3600)));
        $where[] = ['UserID', 'eq',$UserID];
        $where[] = ['RegisterDate', 'between', [$begin_time, $after_time]];
        $res =$this->Db_connect_accounts_info()->where($where)->find();
        if($res){
                return 1;
        }else{
                $map[] = ['LastLoginDate', 'between', [$begin_time, $after_time]];
                $result =$this->Db_connect_accounts_info()->where($map)->find();
                if($result){
                    return 2;
                }else{
                    return 3;
                }
        }
    }
    function burying_insert($point_ID,$SpreaderID,$user_type,$UserID){
            $map=[];
            $map['SpreaderID']=$SpreaderID;
            $map['point_ID']=$point_ID;
            $map['date']=date('Y-m-d',time());
            /*查询打点记录是否存在*/
            $result =$this->Db_connect_game_Burying_point()->field('ID')->where($map)->find();
            if($result){
                $re =$this->Db_connect_game_burying_point_user()
                    ->where('UserID',$UserID)
                    ->where('P_ID',$result['ID'])
                    ->find();
                if(empty($re)){
                    $data['UserID']=$UserID;
                    $data['P_ID']=$result['ID'];
                    $this->Db_connect_game_Burying_point()->where($map)->setInc('Number',1);
                    $user_type =$this->user_type($UserID);
                    $data['type_ID']=$user_type;
                    if($user_type ==1){
                        $this->Db_connect_game_Burying_point()->where($map)->setInc('add_Number',1);
                    }
                    $this->Db_connect_game_burying_point_user()->insert($data);
                }else{
                    $this->Db_connect_game_burying_point_user()
                        ->where('UserID',$UserID)
                        ->where('P_ID',$result['ID'])
                        ->setInc('count',1);
                }
            }else{
                $result=$this->Db_connect_game_Burying_point()->insert($map);
                if($result){
                    $ID =$this->Db_connect_game_Burying_point()->getLastInsID();
                    $re =$this->Db_connect_game_burying_point_user()
                        ->where('UserID',$UserID)
                        ->where('P_ID',$result['ID'])
                        ->find();
                    if(empty($re)){
                        $data['UserID']=$UserID;
                        $data['P_ID']=$ID;
                        $user_type =$this->user_type($UserID);
                        $data['type_ID']=$user_type;
                        if($user_type ==1){
                            $this->Db_connect_game_Burying_point()->where($map)->setInc('add_Number',1);
                        }
                        $this->Db_connect_game_Burying_point()->where($map)->setInc('Number',1);
                        $this->Db_connect_game_burying_point_user()->insert($data);
                    }else{
                        $this->Db_connect_game_burying_point_user()
                            ->where('UserID',$UserID)
                            ->where('P_ID',$result['ID'])
                            ->setInc('count',1);
                    }
                }
            }
            switch ($user_type) {
                case 1:
                    $this->Db_connect_game_Burying_point()->where($map)->setInc('active_count',1);
                    $this->Db_connect_game_Burying_point()->where($map)->setInc('new_count',1);
                    break;
                case 2:
                    $this->Db_connect_game_Burying_point()->where($map)->setInc('active_count',1);
                    break;
            }
            $this->Db_connect_game_Burying_point()->where($map)->setInc('count',1);
            return 'success';
    }



}
